/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.utils;

import com.anhndt.audiobook.business.Resource;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;

/**
 *
 * @author Anhndt
 */
public class DialogUtil {
//    static boolean thirdSoftButton = Display.getInstance().isThirdSoftButton();

    static final int DEFAULT_TIME_OUT = 3000;
    public static int ONE_BUTTON = 1;
    public static int TWO_BUTTON = 2;

    public static Command showInfoDialog(String msg, String[] buttonNames, boolean timeout) {
        return showDialog(msg, ONE_BUTTON, buttonNames, Dialog.TYPE_INFO, Util.getImage("Info"), false, timeout);
    }

    public static Command showInfoDialog(String msg, String[] buttonNames) {
        return showDialog(msg, ONE_BUTTON, buttonNames, Dialog.TYPE_INFO, Util.getImage("Info"), false);
    }

    public static Command showErrorDialog(String msg) {
        return showErrorDialog(msg, new String[]{Resource.CMD_OK});
    }

    public static Command showErrorDialog(String msg, String[] buttonNames) {
        return showDialog(msg, ONE_BUTTON, buttonNames, Dialog.TYPE_ERROR, Util.getImage("Warning"), false);
    }

    public static Command showWarningDialog(String msg, String[] buttonNames, boolean timeout) {
        return showDialog(msg, ONE_BUTTON, buttonNames, Dialog.TYPE_WARNING, Util.getImage("Warning"), false, timeout);
    }

    public static Command showWarningDialog(String msg, String[] buttonNames) {
        return showDialog(msg, ONE_BUTTON, buttonNames, Dialog.TYPE_WARNING, Util.getImage("Warning"), false);
    }

    public static Command showConfirmDialog(String msg, String[] buttonNames) {
        return showDialog(msg, TWO_BUTTON, buttonNames, Dialog.TYPE_CONFIRMATION, Util.getImage("Question"), false);
    }

    public static Command showDialog(String msg, int numButton, String[] buttonNames) {
        return showDialog(msg, numButton, buttonNames, Dialog.TYPE_NONE, null, false);
    }

    public static Command showDialog(String msg, int numButton, String[] buttonNames, int type) {
        return showDialog(msg, numButton, buttonNames, type, null, false);
    }

    public static Command showDialog(String msg, int numButton, String[] buttonNames, int type, Image icon) {
        return showDialog(msg, numButton, buttonNames, type, icon, false);
    }

    public static Command showDialog(String msg, int numButton, String[] buttonNames, int type, Image icon, boolean showCommand) {
        return showDialog(msg, numButton, buttonNames, type, icon, showCommand, false);
    }

    public static Command showDialog(String msg, int numButton, String[] buttonNames, int type, Image icon, boolean showCommand, boolean timeout) {
        if (numButton < 1 || buttonNames == null || numButton != buttonNames.length || StringUtil.isNullOrEmpty(msg)) {
            return null;
        }
        Command[] cmds = new Command[numButton];
        Button[] btns = new Button[numButton];
        Container conButton = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        for (int i = 0; i < numButton; i++) {
            cmds[i] = new Command(buttonNames[i]);
            btns[i] = new Button(cmds[i]);
            btns[i].setPressedStyle(btns[i].getSelectedStyle());
            btns[i].setAlignment(Button.CENTER);
            conButton.addComponent(btns[i]);
        }

        Label lblIcon = new Label(icon);
        lblIcon.setAlignment(Label.CENTER);

        TextArea txtMsg = new TextArea(msg) {

            public int getPreferredH() {
                int lines = getLines();
                int topPadding = getUnselectedStyle().getPadding(false, TOP);
                int bottomPadding = getUnselectedStyle().getPadding(false, BOTTOM);
                int heighFont = getUnselectedStyle().getFont().getHeight();
                return lines * (heighFont + getRowsGap()) + topPadding + bottomPadding;
            }
        };
        txtMsg.setPreferredW(Display.getInstance().getDisplayWidth() - 2 * Display.getInstance().getDisplayWidth() * 5 / 120 - 10);
        txtMsg.setFocusable(false);
        txtMsg.setEditable(false);

        Container conBody = new Container(new BorderLayout());
        conBody.setUIID("DialogContentPane");
        conBody.setScrollable(false);
        conBody.addComponent(BorderLayout.NORTH, lblIcon);
        conBody.addComponent(BorderLayout.CENTER, txtMsg);
        conBody.addComponent(BorderLayout.SOUTH, conButton);

//        conBody.setPreferredH(conBody.getPreferredH() * 5 / 4 );
        conBody.layoutContainer();
//        conBody.revalidate();

        Command[] cmdsInverse = null;
        if (true) {
            cmdsInverse = new Command[numButton];
            for (int i = 0; i < numButton; i++) {
                cmdsInverse[i] = cmds[numButton - 1 - i];
            }
        }
        if (timeout && numButton == ONE_BUTTON) {
            return Dialog.show(null, conBody, cmdsInverse, type, null, DEFAULT_TIME_OUT);
        } else {
            return Dialog.show(null, conBody, cmdsInverse, type, null);
        }

//        System.out.println("txtMSG : " + txtMsg.getLines() + "  font height = " + txtMsg.getUnselectedStyle().getFont().getHeight());
//        System.out.println("txtMSG : " + txtMsg.getPreferredH());
//        System.out.println("conBody : " + conBody.getPreferredH());
//        Dialog dialog = new Dialog();
//        dialog.setLayout(new  BorderLayout());
//        dialog.addComponent(BorderLayout.CENTER,conBody);
//
//
//        int pw = Display.getInstance().getDisplayWidth() * 5 / 120;
//        int ph = (Display.getInstance().getDisplayHeight() - conBody.getPreferredH()) / 2 - 20;
//        //set time out;
//        if (timeout && numButton == ONE_BUTTON) {
//            dialog.setTimeout(DEFAULT_TIME_OUT);
//        }
//        return dialog.show(ph, ph, pw, pw, false, true);
    }

    public static void showDialogInput(String caption, final TextField txt, ActionListener listenerOK) {
        Label lblCaption = new Label(caption);
        lblCaption.getUnselectedStyle().setFont(Util.getBoldFont());
        final Button btnOK = new Button(new Command(Resource.CMD_OK));
        final Button btnCancel = new Button(new Command(Resource.CMD_CANCEL));
        btnOK.setAlignment(Button.CENTER);
        btnCancel.setAlignment(Button.CENTER);
        btnOK.addActionListener(listenerOK);
        Container body = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        body.setUIID("DialogContentPane");
        body.setScrollableX(false);
        body.setScrollableY(true);
        body.addComponent(lblCaption);
        body.addComponent(txt);
        body.addComponent(btnOK);
        body.addComponent(btnCancel);

        Dialog.show(null, body, null);
//        Command[] cmdsInverse = new Command[2];
//        cmdsInverse[0] = btnCancel.getCommand();
//        cmdsInverse[1] = btnOK.getCommand();
//        Dialog.show(null, body, cmdsInverse);
//        dialog.setLayout(new BorderLayout());
//        dialog.addComponent(BorderLayout.CENTER, body);
//        dialog.addCommand(btnCancel.getCommand());
//        dialog.addCommand(btnOK.getCommand());
//        int pw = Display.getInstance().getDisplayWidth() * 5 / 100;
//        int ph = (Display.getInstance().getDisplayHeight() - body.getPreferredH()) / 2;
//
//        dialog.show(ph, ph, pw, pw, false, false);
    }

    public static void showErrorUpdating() {
        showWarningDialog(Resource.TXT_WARRING_UPDATETING, new String[]{Resource.CMD_OK});
    }

    public static void showErrorCheckNetword() {
        showErrorDialog(Resource.TXT_WARRING_NO_NETWORK, new String[]{Resource.CMD_OK});
    }

    public static void showErrorConnectServer() {
        showErrorDialog(Resource.TXT_ERROR_CONNECT_TO_SERVER, new String[]{Resource.CMD_OK});
    }

    public static void showWarningAuthorizeNetwork() {
        showWarningDialog(Resource.TXT_WARRING_AUTHORIZE_NETWORK, new String[]{Resource.CMD_OK});
    }

    public static Command showInfoDialog(String msg, boolean timeout) {
        return showInfoDialog(msg, new String[]{Resource.CMD_OK}, timeout);
    }

    public static Command showInfoDialog(String msg) {
        return showInfoDialog(msg, new String[]{Resource.CMD_OK});
    }

    public static Command showWarningDialog(String msg, boolean timeout) {
        return showWarningDialog(msg, new String[]{Resource.CMD_OK}, timeout);
    }

    public static Command showWarningDialog(String msg) {
        return showWarningDialog(msg, new String[]{Resource.CMD_OK});
    }

    public static Command showConfirmDialog(String msg) {
        return showConfirmDialog(msg, new String[]{Resource.CMD_OK, Resource.CMD_CANCEL});
    }
}
